package com.zmzncs.shop.module.shop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.shop.common.pojo.PageInfo;
import com.zmzncs.shop.common.pojo.Rest;
import com.zmzncs.shop.common.util.JsonUtil;
import com.zmzncs.shop.common.util.JwtUtil;
import com.zmzncs.shop.common.util.RestUtil;
import org.springframework.beans.BeanUtils;
import com.zmzncs.shop.module.shop.pojo.shopinfo.*;
import org.springframework.web.bind.annotation.*;
import com.zmzncs.shop.module.shop.service.ShopInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 任建波
 * @since 2021-05-18
 */
@Api(tags = {"商户-信息"})
@RestController
@RequestMapping("/shop/shop-info")
public class ShopInfoController {

    @Autowired
    private ShopInfoService shopInfoService;


    @ApiOperation(value = "新增")
    @PostMapping("add")
    public Rest add(@RequestBody @Valid ShopInfoAddForm form){
        shopInfoService.add(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("delete")
    public Rest delete(@RequestParam Long id){
        shopInfoService.delete(id);

        return RestUtil.success();
    }

    @ApiOperation(value = "更新")
    @PostMapping("update")
    public Rest update(@RequestBody @Valid ShopInfoUpdateForm form){
        shopInfoService.update(form);

        return RestUtil.success();
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Rest<PageInfo<ShopInfoVO>> page(@Valid ShopInfoSearchForm form){
        IPage<ShopInfoDTO> iPage = shopInfoService.page(form);
        PageInfo<ShopInfoVO> pageInfo = JsonUtil.pageTransform(iPage, ShopInfoVO.class);

        return RestUtil.success(pageInfo);
    }

    @ApiOperation(value = "详情")
    @GetMapping("get-by-id")
    public Rest<ShopInfoVO> getById(@RequestParam Long id){
        ShopInfoDTO shopInfoDTO = shopInfoService.getById(id);

        ShopInfoVO shopInfoVO = new ShopInfoVO();
        BeanUtils.copyProperties(shopInfoDTO, shopInfoVO);

        return RestUtil.success(shopInfoVO);
    }

    @ApiOperation(value = "查询用户管理的所有商户信息")
    @GetMapping("get-by-manage-id")
    public Rest<List<ShopInfoVO>> getByManageId(){
        List<ShopInfoDTO> shopInfoDTOList = shopInfoService.getByManageId();
        List<ShopInfoVO> infoVOList = JsonUtil.listTransform(shopInfoDTOList, ShopInfoVO.class);

        return RestUtil.success(infoVOList);
    }


    @ApiOperation(value = "查询用户管理商户相关信息")
    @GetMapping("get-shop-info-list")
    public Rest<List<ShopInfoTableVO>> getShopInfoList(){
        List<ShopInfoDTO> shopInfoDTOList = shopInfoService.getByManageId();
        List<ShopInfoTableVO> infoVOList = JsonUtil.listTransform(shopInfoDTOList, ShopInfoTableVO.class);

        return RestUtil.success(infoVOList);
    }

}
